package com.zir.face.util;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;import java.util.UUID;
@Component
public class FileUtil {
//该成员变量 可以存储到Constant类中
    private static final String PATH = "C:\\work\\Gitee\\classcode\\Java\\giteedemo\\demotest\\path\\save\\";

    public File storageFile(MultipartFile multipartFile) throws IOException {
        File uploadDir = new File(PATH);
        //如果⽂件⽬录不存在，创建该⽬录
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        // 1.1.返回上传⽂件的原始⽂件名
        String originalFilename = multipartFile.getOriginalFilename();//1.2. 获取后缀名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //1.3.使⽤随机的通⽤唯⼀识别码和扩展名 组合成新的⽂件名
        String newFileName = UUID.randomUUID().toString() + extension;
        //使⽤File.separator ⽂件路径的分隔符，确保跨平台兼容
        File destFile = new File(PATH + File.separator + newFileName);
        //1.4.保存上传⽂件到指定路径
        multipartFile.transferTo(destFile);
        return destFile;
    }
}